//
//  LCSugarListTableViewController.swift
//  JKBiblicalReading
//
//  Created by admin on 2018/8/17.
//  Copyright © 2018年 LC. All rights reserved.
//

import UIKit
import MJRefresh

let sugarListTableCell_Identifier = "sugarListTableCell_Identifier"

class LCSugarListTableViewController: LCViewController{
    // 顶部刷新
    let header = MJRefreshNormalHeader()
    // 底部刷新
    let footer = MJRefreshAutoNormalFooter()
    //id
    @objc var mg = LSZAllDynamicListsMg()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        edgesForExtendedLayout = UIRectEdge.init(rawValue: 0);
        tabBarController?.tabBar.isTranslucent = false;
        view.backgroundColor = UIColor.white
        title = "赠送列表"
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.top.equalTo(view.snp.top).offset(0)
            make.left.right.equalTo(view)
            make.bottom.equalTo(view.snp.bottom)
        }
        // 下拉刷新
        header.setRefreshingTarget(self, refreshingAction: #selector(headerRefresh))
        self.tableView.mj_header = header
        footer.setRefreshingTarget(self, refreshingAction: #selector(footerRefresh))
        self.tableView.mj_footer = footer
        loadData()
    }
    
    // 顶部刷新
    @objc func headerRefresh(){
        self.page = 1
        self.loadData()     // 结束刷新
        self.footer.resetNoMoreData()
    }
    // 底部刷新
    @objc func footerRefresh(){
        self.page = self.page + 1
        self.loadData()        // 结束刷新
    }
    deinit {
        print("deinit");
    }
    //底部视图
    fileprivate lazy var tableView:UITableView = {
        let view = UITableView()
        self.setupComposeTableView(view)//设置
        return view
    }()
    //FIXME:- 下午4:57///设置LCComposeTopView 节圣经
    fileprivate func setupComposeTableView(_ tableView : UITableView){
        //FIXME:- // 设置列表内容
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(LCSugarListTableCell.self, forCellReuseIdentifier: sugarListTableCell_Identifier)
        tableView.tableFooterView = UIView()
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
    }

    lazy var dynamicNewsHot : LCSugarListViewModel = {
        let vm = LCSugarListViewModel()
        return vm
    }()
    
    public lazy var page : NSInteger = {
        var page : NSInteger = NSInteger()
        page = 1;
        return page
    }()
    @objc public lazy var noResultLab : UILabel = {
        let lab : UILabel = UILabel()
        lab.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
        lab.font = UIFont.systemFont(ofSize: 20)
        lab.textColor = UIColor.lightGray
        lab.textAlignment = .center
        lab.isHidden = true
        if !LCUtils.checkReachability() {
            lab.text = "抱歉！没有网络"
        }
        return lab
    }()
//    @objc public lazy var contentStr : String = {
//        let str : String = String()
//        return str
//    }()
}
extension LCSugarListTableViewController: UITableViewDataSource,UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return tableView.cellHeight(for: indexPath, cellContentViewWidth: UIScreen.main.bounds.size.width, tableView: tableView)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        tableView.backgroundView = noResultLab
        if (dynamicNewsHot.dynamicNewsDataMArray.count > 0) {
            noResultLab.isHidden = true
            return dynamicNewsHot.dynamicNewsDataMArray.count
        }else{
            noResultLab.isHidden = false
            return 0
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let sugarListModel = self.dynamicNewsHot.dynamicNewsDataMArray[indexPath.row]
        
        let noImagesCell:LCSugarListTableCell = tableView.dequeueReusableCell(withIdentifier: sugarListTableCell_Identifier) as! LCSugarListTableCell
        noImagesCell.model = sugarListModel as? LCSugarListModel
        noImagesCell.authorImageView.tapGesture { [weak self] (_)  in
            self?.addtimeButtonClick(sugarListModel: sugarListModel as! LCSugarListModel)
        }
        noImagesCell.usernameButton.tapGesture { [weak self] (_) in
            self?.addtimeButtonClick(sugarListModel: sugarListModel as! LCSugarListModel)
        }
//        noImagesCell.addtimeButton.tapGesture { [weak self] (_) in
//            self?.addtimeButtonClick(sugarListModel: sugarListModel as! LCSugarListModel)
//        }

//        noImagesCell.usernameButton.addTarget(self, action: #selector(titleButtonClick), for: .touchUpInside)
//        noImagesCell.addtimeButton.addTarget(self, action: #selector(addtimeButtonClick), for: .touchUpInside)

        noImagesCell.selectionStyle = .none
        return noImagesCell

    }

    @objc fileprivate func addtimeButtonClick(sugarListModel : LCSugarListModel){
        print("地区和add时间")
        let vc: LSZPersonalInformationPageVC = LSZPersonalInformationPageVC()
        vc.uid = sugarListModel.user.uid;
        vc.imageViewHeader = sugarListModel.user.avatar;
        navigationController?.pushViewController(vc, animated: true)
    }

}

extension LCSugarListTableViewController{
    func endRefreshing(){
        self.tableView.mj_header.endRefreshing()
        self.tableView.mj_footer.endRefreshing()
    }

    fileprivate func loadData(){
        dynamicNewsHot.getNewsComposeTextData(with:[mg.uid,mg.id_id], page: page, success: {[weak self] (res) in
            if (self?.dynamicNewsHot.dynamicNewsDataMArray.count)! > 0 {
                self?.endRefreshing()
                self?.tableView.reloadData()
            }else{
                print("请求失败");
            }
        },failure: {[weak self] (error) in
                self?.endRefreshing()
                print(error ?? "")
                print("请求失败");
        }) {[weak self] () in
            self?.footer.endRefreshingWithNoMoreData()
        }
    }

}
